/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.supplier.controller;

import java.util.ArrayList;
import java.util.List;
import queenb.net.entitybean.T025;
import queenb.web.controller.EmployeeController;
import queenb.web.controller.QueenbController;
import queenb.web.supplier.store.T025Store;
import queenb.web.util.ContextUtil;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class T025Controller extends QueenbController {

    private int PT025;
    private int REFID;
    private String TV026;
    private String TV027;// Code Ngan hang  
    private String TV028;// Ngoai te
    private String TV029;// Ngan hang
    private String TV030;// Dia chi
    private T025Store t025Store;
    private String pagereload;
    private boolean flag;
    private String displayN100;
    private String displayK100;
    private boolean add = false;
    private boolean edit = false;
    private boolean delete = false;
    private String table = "";
    private List<T025> listdata = new ArrayList<T025>();
    private String messageform = "";
    private List<T025> listAjaxT025Bean;
    private int render;
    private int beginContructor;
     private List<T025> listoftabT025 = new ArrayList<T025>();// list tài khaonr của Nhanv ien
    public T025Controller() {
        t025Store = new T025Store();
        Object display = getRequest().getParameter("page");

        try {
            if (display.equals("employee.jsp")) {
                // key de nhan biet dau la N100 va K100
                this.displayN100 = "block;";
                this.displayK100 = "none;";
                if (getLoginSession().getRight().isEMP_ACC_CRE()) {
                    add = true;
                }
                if (getLoginSession().getRight().isEMP_ACC_MOD()) {
                    edit = true;
                }
                if (getLoginSession().getRight().isEMP_ACC_DEL()) {
                    delete = true;
                }
            } else if (display.equals("supplier.jsp")) {
                this.displayN100 = "none;";
                this.displayK100 = "block;";
                if (getLoginSession().getRight().isSUP_ACC_CRE()) {
                    add = true;
                }
                if (getLoginSession().getRight().isSUP_ACC_MOD()) {
                    edit = true;
                }
                if (getLoginSession().getRight().isSUP_ACC_DEL()) {
                    delete = true;
                }
            } else if (display.equals("customer.jsp")) {
                this.displayN100 = "block;";
                this.displayK100 = "none;";
                if (getLoginSession().getRight().isCUS_ACC_CRE()) {
                    add = true;
                }
                if (getLoginSession().getRight().isCUS_ACC_MOD()) {
                    edit = true;
                }
                if (getLoginSession().getRight().isCUS_ACC_DEL()) {
                    delete = true;
                }
            } else if (display.equals("cuscompany.jsp")) {
                this.displayN100 = "none;";
                this.displayK100 = "block;";
                if (getLoginSession().getRight().isCOM_ACC_CRE()) {
                    add = true;
                }
                if (getLoginSession().getRight().isCOM_ACC_MOD()) {
                    edit = true;
                }
                if (getLoginSession().getRight().isCOM_ACC_DEL()) {
                    delete = true;
                }
            }else if (display.equals("partner.jsp")) {
                 this.displayN100 = "none;";
                this.displayK100 = "block;";
                if (getLoginSession().getRight().isCOM_ACC_CRE()) {
                    add = true;
                }
                if (getLoginSession().getRight().isCOM_ACC_MOD()) {
                    edit = true;
                }
                if (getLoginSession().getRight().isCOM_ACC_DEL()) {
                    delete = true;
                }
            }
        } catch (Exception ex) {
        }

        try {
            this.pagereload = display.toString();
            if (this.pagereload == null) {
                this.pagereload = ContextUtil.getRequestParameter("page");
            }
            //this.pagereload = pages.toString();
            System.out.println("**************************" + pagereload);
        } catch (Exception ex) {
        }
        Object objPN100 = getRequest().getParameter("pn100");
        if (objPN100 != null) {
            this.REFID = Integer.parseInt(objPN100.toString());
            System.out.println("----------pn100------------" + REFID);
        }
        Object objTV026 = getRequest().getParameter("tv026");
        if (objTV026 != null) {
            this.TV026 = objTV026.toString();

        }

        Object pnREFID = getRequest().getParameter("id");
        Object tv026Object = getRequest().getParameter("itemid");
        Object pt025Object = getRequest().getParameter("pt025");
        Object objAction = getRequest().getParameter("action");
        if (pnREFID != null) {
            this.REFID = Util.parseInt(pnREFID);
            System.out.println("--------------" + REFID);

            if (tv026Object != null) {
                this.TV026 = tv026Object.toString();
            }
        }
        if (objAction != null) {

            if (objAction.equals("saveT025")) {
              //  this.REFID = EmployeeController.idEmp;
            } else if (objAction.equals("editajaxpage")) {
                List<T025> listNew = null;
                System.out.println("-----du lieu contaat ---");
                if (listNew == null) {
                    System.out.println("----" + 0 + "," + REFID + "," + TV026 + "," + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + getLogin().getUserName());
                    listNew = t025Store.getListT025All(0, REFID, TV026, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());

                }
                listAjaxT025Bean = new ArrayList<T025>();
                if (listNew != null) {

                    listAjaxT025Bean = listNew;

                    if (listAjaxT025Bean.size() > 0) {
                        render = 1;
                    } else {
                        render = 0;
                    }
                }

            } else if (objAction.equals("updateajaxT025")) {
                Object objTV027 = getRequest().getParameter("TV027");
                if (objTV027 != null) {
                    this.TV027 = objTV027.toString();
                }
                Object objTV028 = getRequest().getParameter("TV028");
                if (objTV028 != null) {
                    this.TV028 = objTV028.toString();
                }
                Object objTV029 = getRequest().getParameter("TV029");
                if (objTV029 != null) {
                    this.TV029 = objTV029.toString();
                }
                Object objTV030 = getRequest().getParameter("TV030");
                if (objTV030 != null) {
                    this.TV030 = objTV030.toString();
                }
                Object objPT025 = getRequest().getParameter("PT025");
                if (objPT025 != null) {
                    this.PT025 = Integer.parseInt(objPT025.toString());
                }
              //  Object objTV026 = getRequest().getParameter("tv026");
               
                updateT025();

            } else if (objAction.equals("add")) {
                this.REFID = Util.parseInt(pnREFID);
                System.out.println("--------------" + REFID);
                if (tv026Object != null) {
                    this.TV026 = tv026Object.toString();
                }
                this.flag = true;
            } else if (objAction.equals("edit")) {
                flag = false;
                this.REFID = Util.parseInt(pnREFID);
                System.out.println("---bbbbbbbbbbb-----------" + REFID);
                if (tv026Object != null) {
                    this.TV026 = tv026Object.toString();
                }
                this.PT025 = Util.parseInt(pt025Object);
                List<T025> listT025 = getListT025All();
                if (listT025 != null && listT025.size() > 0) {
                    this.PT025 = listT025.get(0).getPT025();
                    this.REFID = listT025.get(0).getREFID();
                    this.TV026 = listT025.get(0).getTV026();
                    this.TV027 = listT025.get(0).getTV027();
                    this.TV028 = listT025.get(0).getTV028();
                    this.TV029 = listT025.get(0).getTV029();
                    this.TV030 = listT025.get(0).getTV030();
                }
            }
        }

    }

    // Ham get Data
    public List<T025> getListT025All() {
        try {

            if (listdata == null || listdata.isEmpty()) {
                  if(SessionProvider.getSessionMapValue("PN100HD")!=null){
                REFID = Util.parseInt(SessionProvider.getSessionMapValue("PN100HD"));
            }
                System.out.println("---list ---" + PT025 + "," + REFID + "," + TV026 + "," + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + getLogin().getUserName());
                List<T025> listT025 = t025Store.getListT025All(PT025, REFID, TV026, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
                listdata = listT025;
                System.out.println("---->" + listT025.size());
            }
            return listdata;
        } catch (Exception ex) {
            return null;
        }
    }

    public void updateT025() {
        System.out.println("----fn100-------------" + this.REFID + "---pnt025" + PT025);
        if (TV027 == null || TV027.equals("")) {
            setMessageform("(*) không được để trống!");
        } else {
            if (flag == true) {
                int count = t025Store.updateT025(PT025, REFID, TV026, TV027, TV028, TV029, TV030, 0, 0, 0, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
                if (count > 0) {
                    setMessageform(getResourceString("success"));
                   // showGlobalReport(true, getResourceString("datasavesuccessful"));
                } else {
                    setMessageform(getResourceString("error"));
                   // showGlobalReport(true, getResourceString("datasuccessfail"));
                }
            } else {
                System.out.println("-----in"+PT025+","+ REFID+","+ TV026+","+ TV027+","+ TV028+","+ TV029+","+ TV030+","+ 0+","+ 0+","+ 0+","+ getLogin().getModule()+","+ getLogin().getCompanyNo()+","+ getLogin().getUserName());
                int count = t025Store.updateT025(PT025, REFID, TV026, TV027, TV028, TV029, TV030, 0, 0, 0, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
                if (count > 0) {
                    setMessageform(getResourceString("success"));
                   // showGlobalReport(true, getResourceString("datasavesuccessful"));
                } else {
                   setMessageform(getResourceString("error"));
                  //  showGlobalReport(true, getResourceString("datasuccessfail"));
                }
            }
            if (pagereload != null) {
                 showGlobalReport(true, getResourceString("datasavesuccessful"));
                   // sendRedirectURL(pagereload, REFID, 5);
                    sendRedirectURLnew(pagereload, "itemcontent", REFID);
//                if (pagereload.equals("employee.jsp")) {
//                     showGlobalReport(true, getResourceString("datasavesuccessful"));
//                    //Reaload lai tung trang : Thi du: Neu la page employee.jsp, thi no reload lai chinh trang do va id do
//                    sendRedirectURL(pagereload, REFID, 5);
//                } else if (pagereload.equals("supplier.jsp")) {
//                     showGlobalReport(true, getResourceString("datasavesuccessful"));
//                    sendRedirectURL(pagereload, REFID, 5);
//                } else if (pagereload.equals("customer.jsp")) {
//                     showGlobalReport(true, getResourceString("datasavesuccessful"));
//                   // sendRedirectURL(pagereload, REFID, 7);
//                      sendRedirectURLnew(pagereload, "itemcontent", REFID);
//                } else if (pagereload.equals("cuscompany.jsp")) {
//                     showGlobalReport(true, getResourceString("datasavesuccessful"));
//                    sendRedirectURLnew(pagereload, "itemcontent", REFID);
//                }else if (pagereload.equals("partner.jsp")) {
//                     showGlobalReport(true, getResourceString("datasavesuccessful"));
//                    sendRedirectURL(pagereload, REFID, 5);
//                }else if (pagereload.equals("employeenew.jsp")) {
//                     showGlobalReport(true, getResourceString("datasavesuccessful"));
//                   // sendRedirectURL(pagereload, REFID, 5);
//                     sendRedirectURLnew(pagereload, "itemcontent", REFID);
//                }
            }
        }
    }

    public int deleteT025(int pn, int id, String item) {
        if (pn != 0 && id != 0 && !item.equals("")) {
            int rows = 0;
            rows = t025Store.deleteT025(pn, id, item, getLogin().getUserName());
            return rows;
        } else {
            return 0;
        }
    }

    /**
     * @return the PT025
     */
    public int getPT025() {
        return PT025;
    }

    /**
     * @param PT025 the PT025 to set
     */
    public void setPT025(int PT025) {
        this.PT025 = PT025;
    }

    /**
     * @return the REFID
     */
    public int getREFID() {
         if(REFID==0){
           if(SessionProvider.getSessionMapValue("PN100HD")!=null){
                REFID = Util.parseInt(SessionProvider.getSessionMapValue("PN100HD"));
            }
        }
        return REFID;
    }

    /**
     * @param REFID the REFID to set
     */
    public void setREFID(int REFID) {
        this.REFID = REFID;
    }

    /**
     * @return the TV026
     */
    public String getTV026() {
        return TV026;
    }

    /**
     * @param TV026 the TV026 to set
     */
    public void setTV026(String TV026) {
        this.TV026 = TV026;
    }

    /**
     * @return the TV027
     */
    public String getTV027() {
        return TV027;
    }

    /**
     * @param TV027 the TV027 to set
     */
    public void setTV027(String TV027) {
        this.TV027 = TV027;
    }

    /**
     * @return the TV028
     */
    public String getTV028() {
        return TV028;
    }

    /**
     * @param TV028 the TV028 to set
     */
    public void setTV028(String TV028) {
        this.TV028 = TV028;
    }

    /**
     * @return the TV029
     */
    public String getTV029() {
        return TV029;
    }

    /**
     * @param TV029 the TV029 to set
     */
    public void setTV029(String TV029) {
        this.TV029 = TV029;
    }

    /**
     * @return the TV030
     */
    public String getTV030() {
        return TV030;
    }

    /**
     * @param TV030 the TV030 to set
     */
    public void setTV030(String TV030) {
        this.TV030 = TV030;
    }

    /**
     * @return the pagereload
     */
    public String getPagereload() {
        return pagereload;
    }

    /**
     * @param pagereload the pagereload to set
     */
    public void setPagereload(String pagereload) {
        this.pagereload = pagereload;
    }

    /**
     * @return the displayN100
     */
    public String getDisplayN100() {
        return displayN100;
    }

    /**
     * @param displayN100 the displayN100 to set
     */
    public void setDisplayN100(String displayN100) {
        this.displayN100 = displayN100;
    }

    /**
     * @return the displayK100
     */
    public String getDisplayK100() {
        return displayK100;
    }

    /**
     * @param displayK100 the displayK100 to set
     */
    public void setDisplayK100(String displayK100) {
        this.displayK100 = displayK100;
    }

    /**
     * @return the add
     */
    public boolean isAdd() {
        return add;
    }

    /**
     * @param add the add to set
     */
    public void setAdd(boolean add) {
        this.add = add;
    }

    /**
     * @return the edit
     */
    public boolean isEdit() {
        return edit;
    }

    /**
     * @param edit the edit to set
     */
    public void setEdit(boolean edit) {
        this.edit = edit;
    }

    /**
     * @return the delete
     */
    public boolean isDelete() {
        return delete;
    }

    /**
     * @param delete the delete to set
     */
    public void setDelete(boolean delete) {
        this.delete = delete;
    }

    /**
     * @return the table
     */
    public String getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(String table) {
        this.table = table;
    }

    /**
     * @return the listdata
     */
    public List<T025> getListdata() {
        return listdata;
    }

    /**
     * @param listdata the listdata to set
     */
    public void setListdata(List<T025> listdata) {
        this.listdata = listdata;
    }

    /**
     * @return the messageform
     */
    public String getMessageform() {
        return messageform;
    }

    /**
     * @param messageform the messageform to set
     */
    public void setMessageform(String messageform) {
        this.messageform = messageform;
    }

    /**
     * @return the listAjaxT025Bean
     */
    public List<T025> getListAjaxT025Bean() {
        return listAjaxT025Bean;
    }

    /**
     * @param listAjaxT025Bean the listAjaxT025Bean to set
     */
    public void setListAjaxT025Bean(List<T025> listAjaxT025Bean) {
        this.listAjaxT025Bean = listAjaxT025Bean;
    }

    /**
     * @return the render
     */
    public int getRender() {
        return render;
    }

    /**
     * @param render the render to set
     */
    public void setRender(int render) {
        this.render = render;
    }

    /**
     * @return the beginContructor
     */
    public int getBeginContructor() {
        return beginContructor;
    }

    /**
     * @param beginContructor the beginContructor to set
     */
    public void setBeginContructor(int beginContructor) {
        this.beginContructor = beginContructor;
    }

    /**
     * @return the listoftabT025
     */
    public List<T025> getListoftabT025() {
        try {
            if(SessionProvider.getSessionMapValue("PN100HD")!=null){
                REFID = Util.parseInt(SessionProvider.getSessionMapValue("PN100HD"));
            }
            listoftabT025  = t025Store.getListT025All(PT025, REFID, "N100", getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
              System.out.println("-----SIZE---------"+listoftabT025.size());
        } catch (Exception e) {
        }
        return listoftabT025;
    }
    
     public List<T025> getListoftabT025Cus() {
        try {
            if(SessionProvider.getSessionMapValue("PN100HD")!=null){
                REFID = Util.parseInt(SessionProvider.getSessionMapValue("PN100HD"));
            }
            listoftabT025  = t025Store.getListT025All(PT025, REFID, "K100", getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
              System.out.println("-----SIZE---------"+listoftabT025.size());
        } catch (Exception e) {
        }
        return listoftabT025;
    }

    /**
     * @param listoftabT025 the listoftabT025 to set
     */
    public void setListoftabT025(List<T025> listoftabT025) {
        this.listoftabT025 = listoftabT025;
    }
}
